package cs2340.todolist.controller.account_activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import cs2340.todolist.R;
import cs2340.todolist.application.ToDoListApplication;
import cs2340.todolist.controller.HomeScreenActivity;
import cs2340.todolist.model.account_management.AccountManagementMessageHandler;
import cs2340.todolist.model.account_management.AccountManager;
import cs2340.todolist.model.account_management.AccountManager.AccountManagementMessage;

public class CreateAccountActivity extends Activity {
	
	private AccountManager accountManager;
	
    /** Called when the CreateAccount is first created.
     * @param savedInstanceState
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_account);
        
        //creates variables for all the text fields on the GUI
        final EditText editName 		= (EditText)findViewById(R.id.edit_name);
        final EditText editEmail 		= (EditText)findViewById(R.id.edit_email);
        final EditText editUsername 	= (EditText)findViewById(R.id.edit_username);
        final EditText editPassword 	= (EditText)findViewById(R.id.edit_password);
        final EditText retypePassword 	= (EditText)findViewById(R.id.retype_password);
        
        //access the cancel button object using id
        // and then adds a listener to it
        final Button cancelAccountCreationButton = (Button) findViewById(R.id.cancel_create_account_button);
        cancelAccountCreationButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				//this brings up the login screen if the cancel button is pressed
				Intent loginScreenIntent = new Intent(CreateAccountActivity.this,LoginScreenActivity.class);
				startActivity(loginScreenIntent);
			}
        });
        
        
        //access the finish button object using id
        final Button finishAccountCreationButton = (Button) findViewById(R.id.finish_create_account_button);
        finishAccountCreationButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				//tries to create a new account
				AccountManagementMessage createAccountMessage = accountManager.tryToCreateAccount(
						editName.getText().toString(),
						editEmail.getText().toString(),
						editUsername.getText().toString(),
						editPassword.getText().toString(),
						retypePassword.getText().toString()
						);
				//handles the message based on what happened before
				new AccountManagementMessageHandler(CreateAccountActivity.this) {
					@Override
					public void handleMessage(final AccountManagementMessage message) {
			        	//go back to login
			        	
						AlertDialog.Builder builder = new AlertDialog.Builder(context);
						builder	.setMessage(message.getMessage())
								.setCancelable(false)
								.setNeutralButton("OK", new DialogInterface.OnClickListener() {
						           public void onClick(DialogInterface dialog, int id) {
						               dialog.cancel();
						               if(message.getMessage().equals(AccountManagementMessage.ACCOUNT_CREATED_MESSAGE)) {
						            	   accountManager.tryToLogin( editUsername.getText().toString(), editPassword.getText().toString());
										   Intent goToHomeScreen = new Intent(CreateAccountActivity.this,LoginScreenActivity.class);
										   startActivity(goToHomeScreen);
						               }
						           }
						       });
						
						builder.create().show();
					   	
			        }
				}.handleMessage(createAccountMessage);
			}
        });
        
    }
    
    /**
     * called whenever this activity is started
     */
    public void onStart() {
    	super.onStart();

    	//grabs the account manager from the background application
        accountManager = ((ToDoListApplication)getApplication()).getSessionManager().getAccountManager();
        accountManager.changeContext(this);
    	
    }
    
 
}
